package gazzera.node.core.peer;

import gazzera.node.core.*;
import gazzera.node.core.events.*;

class PeerReader implements Runnable
{
	private Peer peer;
	private ICore core;
	
	public PeerReader(Peer peer, ICore core)
	{
		super();
		this.peer = peer;
		this.core = core;
	}
	
	@Override
	public void run()
	{
		try
		{
			while(true)
			{
			    Event e = new MessageEvent(peer, peer.recv());
			    if(core.isBusy()) 
			    {
				Thread.sleep(100);
    			    }
			    core.enqueue(e);
			}
		}
		catch (Exception e)
		{
			
			core.enqueue(new DisconnectionEvent(peer));
		}

	}

}
